package com.mic.yzzn.plc.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mic.yzzn.plc.model.*;


@RestController("pojoController")
@RequestMapping("/pojo")
public class PojoController {

	/**
	 * 用于构造PLCReadRequest的JSON结构
	 * 仅能读一个Station(PLC)
	 * 可同时读多个离散点
	 * 可同时读多个连续点位
	 * @return
	 */
	@RequestMapping("/PLCReadRequest")
	public ResponseEntity<PLCReadRequest> PLCReadRequest() {
		
		Date time = new Date();

		String agent = "unknow";

		Station station = new Station("plc","02");

	    List<ContactPoint> pointList =  new ArrayList<ContactPoint>();
	    pointList.add(new ContactPoint("R","2000"));
	    pointList.add(new ContactPoint("R","2002"));
	    pointList.add(new ContactPoint("D","20000"));

	    List<ContactSection> sectionList = new ArrayList<ContactSection>();
	    sectionList.add(new ContactSection("R", "2000", "2050"));
	    sectionList.add(new ContactSection("D", "22000", "22500"));
	    
		PLCReadRequest readRequest = new PLCReadRequest(time, agent, station,pointList,sectionList);
		
		return new ResponseEntity<PLCReadRequest>(readRequest, HttpStatus.OK);
		
	}
	
	/**
	 * 用于构造PLCWriteRequest的JSON结构
	 * 仅能写一个Station(PLC)
	 * 可同时写多个点位
	 * @return
	 */
	@RequestMapping("/PLCWriteRequest")
	public ResponseEntity<PLCWriteRequest> PLCWriteRequest() {
		
		PLCWriteRequest writeRequest = new PLCWriteRequest();
		
		Date time = new Date();
		writeRequest.setTime(time);
		
		String agent = "unknow";
		writeRequest.setAgent(agent);
		
		Station station = new Station("plc","02");
		writeRequest.setStation(station);
		
		List<ContactPointData> dataList = new ArrayList<ContactPointData>();
		
		ContactPointData pointData = new ContactPointData();
		ContactPoint point = new ContactPoint("R","2000");
		pointData.setContactPoint(point);
		pointData.setValue("1012");
		dataList.add(pointData);
		
		dataList.add(new ContactPointData("R","2001","1013"));
		
		writeRequest.setDataList(dataList);
		
		return new ResponseEntity<PLCWriteRequest>(writeRequest, HttpStatus.OK);
		
	}
	
	/**
	 * 用于构造PLCStatusRequest的JSON结构
	 * 可同时查询多个Station(PLC)的状态
	 * @return
	 */
	@RequestMapping("/PLCStatusRequest")
	public ResponseEntity<PLCStatusRequest> PLCStatusRequest(){
		
		PLCStatusRequest statusRequest = new PLCStatusRequest();
		Date time = new Date();
		statusRequest.setTime(time);
		
		String agent = "unknow";
		statusRequest.setAgent(agent);
		
		Station station = new Station("plc","02");
		List<Station> stationList =  new ArrayList<Station>();
		stationList.add(station);
		
		//stationList.add(new Station("plc", "222.201.167.253:9094"));
		
		statusRequest.setStationList(stationList);
		
		return new ResponseEntity<PLCStatusRequest>(statusRequest, HttpStatus.OK);
	}
	
	
}
